﻿using Common.Models;
using Common.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Identity.Client;
using Minio.DataModel;
using Wallpaper.Models.DTOs.Requests.Wallpaper;
using Wallpaper.Models.DTOs.Requests.WallPaper;
using Wallpaper.Models.DTOs.Responses.Wallpaper;
using Wallpaper.Models.Entities;
using Wallpaper.Services.IService;

namespace WallpaperApi.Controllers
{
    [Route("api/wallpapers")]
    [ApiController]
    [Authorize]
    public class WallpapersController : BaseController
    {
        readonly IWallpaperService _wallpaperService;
        readonly ILogger<WallpapersController> _logger;


        public WallpapersController(IWallpaperService wallpaperService, ILogger<WallpapersController> logger)
        {
            _wallpaperService = wallpaperService;
            _logger = logger;
        }



        /// <summary>
        /// 后台上传壁纸
        /// </summary>
        /// <param name="upload"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResponse<Object>> Uploadwallpaper([FromBody] UploadWallpaperRequest upload)
        {
            bool result = await _wallpaperService.UploadWallpaperAsync(upload, AdminId);
            return ApiResponse<Object>.Success();
        }


        /// <summary>
        /// 后台修改
        /// </summary>
        /// <param name="upload"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ApiResponse<Object>> Update(string id, [FromBody] UpdateWallpaperRequest request)
        {
            bool result = await _wallpaperService.UpdatedWallpaperAsync(id, request);
            return ApiResponse<Object>.Success();
        }

        /// <summary>
        /// 删除壁纸
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ApiResponse<Object>> Delete(string id)
        {
            bool result = await _wallpaperService.DeleteByIdAsync(id);
            return ApiResponse<Object>.Success();
        }




        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="wallpaperQueryRequest"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResponse<PagedResult<WallpaperPageResponse>>> Page([FromQuery] WallpaperQueryRequest wallpaperQueryRequest)
        {
            PagedResult<WallpaperPageResponse> waitForChangedResults = await _wallpaperService.PageAsync(wallpaperQueryRequest);
            return ApiResponse<PagedResult<WallpaperPageResponse>>.Success(waitForChangedResults);
        }


        /// <summary>
        /// 小程序搜索
        /// </summary>
        /// <param name="searchRequest"></param>
        /// <returns></returns>
        [HttpGet("search")]
        [AllowAnonymous]
        public async Task<ApiResponse<PagedResult<QueryWallpaperResponse>>> SearchWallpapers([FromQuery] SearchRequest searchRequest)
        {
            PagedResult<QueryWallpaperResponse> pagedResult = await _wallpaperService.SearchWallpapersAsync(searchRequest);
            return ApiResponse<PagedResult<QueryWallpaperResponse>>.Success(pagedResult);
        }

        /// <summary>
        /// 小程序首页壁纸
        /// </summary>
        /// <returns></returns>
        [HttpGet("index")]
        [AllowAnonymous]
        public async Task<ApiResponse<PagedResult<WallpapersResponse>>> Index([FromQuery] PageRequest request)
        {
            PagedResult<WallpapersResponse> pagedResult = await _wallpaperService.IndexAsync(request);
            return ApiResponse<PagedResult<WallpapersResponse>>.Success(pagedResult);
        }


        /// <summary>
        /// 小程序上传壁纸
        /// </summary>
        /// <returns></returns>
        [HttpPost("app-upload")]
        public async Task<ApiResponse<Object>> AppUploadWallpaper([FromBody] AppUploadWallpaperRequest request) 
        {
            bool result = await _wallpaperService.AppUploadWallpaperAsync(request, UserId);
            return ApiResponse<Object>.Success();
        }

        /// <summary>
        /// 后端审核小程序壁纸上传
        /// </summary>
        /// <param name="id"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPatch("{id}/review")]
        public async Task<ApiResponse<Object>> ReviewUserWallpaper(string id,
            [FromBody] ReviewWallpaperRequest request)
        {

            await _wallpaperService.ReviewUserWallpaperAsync(id, AdminId, request);
            return ApiResponse<Object>.Success();
        }



        /// <summary>
        /// 获取用户上传的壁纸
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet("review-list")]
        public async Task<ApiResponse<PagedResult<QueryWallpaperResponse>>> GetReviewWallpapers([FromQuery] UserWallpapersRequest request)

        {
            // 业务逻辑
            PagedResult<QueryWallpaperResponse> result = await _wallpaperService.GetWallpapersAsync(request.ReviewStatus, UserId, request.Page,request.PageSize);
            return ApiResponse<PagedResult<QueryWallpaperResponse>>.Success(result);
        }



        /// <summary>
        /// 根据id获取壁纸详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}/detail")]
        public async Task<ApiResponse<DetailResponse>> GetWallpaperDetail(string id)
        {
            DetailResponse detailResponse= await _wallpaperService.GetWallpaperDetailAsync(id);
            return ApiResponse<DetailResponse>.Success(detailResponse);

        }
    }

}
